package com.ruoyi.common.utils.sign;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedHashSet;

/**
 * @author xumh
 * @title: PasswordUtil 判断是否为弱口令
 * @projectName ruoyi
 * @description: TODO
 * @date 2024/12/1215:43
 */
public class PasswordUtil {
    public PasswordUtil() {
    }

    public static String md5(String message) {
        MessageDigest messageDigest = null;

        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(message.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException var5) {
            var5.printStackTrace();
        } catch (UnsupportedEncodingException var6) {
            var6.printStackTrace();
        }

        byte[] byteArray = messageDigest.digest();
        StringBuffer md5StrBuff = new StringBuffer();

        for(int i = 0; i < byteArray.length; ++i) {
            if (Integer.toHexString(255 & byteArray[i]).length() == 1) {
                md5StrBuff.append("0").append(Integer.toHexString(255 & byteArray[i]));
            } else {
                md5StrBuff.append(Integer.toHexString(255 & byteArray[i]));
            }
        }

        return md5StrBuff.toString();
    }

    public static boolean isWeekPassword(String password) {
        password = password.trim();
        if (password.length() <= 6) {
            return true;
        } else {
            char[] chPassword = password.toCharArray();
            LinkedHashSet<Character> link = new LinkedHashSet();
            StringBuilder buff = new StringBuilder();
            boolean result = false;
            int count = 0;

            for(int i = 0; i < chPassword.length; ++i) {
                int preNumber = i == 0 ? -999 : chPassword[i - 1] - 0;
                int currentNumber = chPassword[i] - 0;
                int asciiNum = preNumber + currentNumber;
                if ((96 <= asciiNum && asciiNum <= 114 || 130 <= asciiNum && asciiNum <= 180 || 194 <= asciiNum && asciiNum <= 244) && Math.abs(preNumber - currentNumber) == 1) {
                    ++count;
                }

                if (!link.contains(chPassword[i])) {
                    link.add(chPassword[i]);
                    buff.append(chPassword[i]);
                }
            }

            String resultStr = String.valueOf(buff);
            if (count == chPassword.length - 1 || resultStr.length() <= 1) {
                result = true;
            }

            return result;
        }
    }

    public static void main(String[] args) {
        String password = "123";
        boolean weekPassword = isWeekPassword(password);
        if (weekPassword) {
            System.out.println("弱口令");
        } else {
            System.out.println("强口令");
        }

    }
}
